package com.lyc.mall.controller.admin;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lyc.mall.model.BO.AddGoodsBO;
import com.lyc.mall.model.PO.Type;
import com.lyc.mall.model.VO.GetGoodsByTypeVO;
import com.lyc.mall.service.GoodsService;
import com.lyc.mall.service.impl.GoodsServiceImpl;
import com.lyc.mall.util.FileUploadUtils;
import com.lyc.mall.util.HttpUtils;
import com.lyc.mall.util.Result;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {
    Gson gson = new Gson();

    private GoodsService goodsService = new GoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = HttpUtils.getOperation(req);
        if ("getType".equals(operation)) {
            getType(req, resp);
        } else if ("getGoodsByType".equals(operation)) {
            getGoodsByType(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = HttpUtils.getOperation(req);
        if ("imgUpload".equals(operation)) {
            imgUpload(req, resp);
        } else if ("addGoods".equals(operation)) {
            addGoods(req, resp);
        }
    }

    /**
     * 业务逻辑
     * 1、获取请求参数
     * 2、实现业务逻辑
     * 3、返回数据
     *
     * @param request
     * @param response
     */
    public void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = goodsService.getType();
        response.getWriter().println(gson.toJson(Result.OK(typeList)));
    }

    /**
     * 在请求地址栏后附着了?typeId=1
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void getGoodsByType(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String typeId = req.getParameter("typeId");
        if (StringUtils.isEmpty(typeId)) {
            resp.getWriter().println(gson.toJson(Result.ERROR("参数不能为空")));
            return;
        }
        List<GetGoodsByTypeVO> getGoodsByTypeVOList = goodsService.getGoodsByType(typeId);
        resp.getWriter().println(gson.toJson(Result.OK(getGoodsByTypeVOList)));
    }

    /**
     * admin上传新增的商品图片
     *
     * @param req
     * @param resp
     */
    private void imgUpload(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> map = FileUploadUtils.parseRequest(req);
        String path = map.get("file");
        resp.getWriter().println(gson.toJson(Result.OK(path)));
    }

    /**
     * 新增商品的业务逻辑
     * 1、获取页面提交过来的请求参数 json
     * 2、处理具体的业务逻辑 主要就是插入两张表
     * 3、返回响应
     *
     * @param req
     * @param resp
     */
    private void addGoods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtils.getRequestBody(req);
        AddGoodsBO addGoodsBO = null;
        try {
            addGoodsBO = gson.fromJson(requestBody, AddGoodsBO.class);
        } catch (JsonSyntaxException e) {
            resp.getWriter().println(gson.toJson(Result.ERROR("参数不合法")));
            return;
        }
        goodsService.addGoods(addGoodsBO);
        resp.getWriter().println(gson.toJson(Result.OK()));
    }
}
